package edu.bbu.cshu.wmpc.dao.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.log4j.Level;

import edu.bbu.cshu.wmpc.loggin.EventLogger;
/**
 * <ul>
 * <li><b><i>ConnectionManager</i></b></li>
 * <li style="list-style: none;"></li>
 * <li style="list-style: none;">
 * Enumeration that manages connections with a database. The connection settings a predefined.
 * 
 * @author WMPC team
 * </li>
 * </ul>
 */ 
public enum ConnectionManager {
	/**
	 * <ul>
	 * <li><b><i>INSTANCE</i></b></li>
	 * <li style="list-style: none;"></li>
	 * <li style="list-style: none;">
	 * Ensures that only a single instance of the enumeration exists.
	 * 
	 * </li>
	 * </ul>
	 */
	INSTANCE;
	/**
	 * <ul>
	 * <li><b><i>server</i></b></li>
	 * <li style="list-style: none;"></li>
	 * <li style="list-style: none;">
	 * {@link String} name of the database {@code server} to which a {@link Connection} will be opened.
	 * 
	 * </li>
	 * </ul>
	 */
	private static String server = "localhost";
	/**
	 * <ul>
	 * <li><b><i>port</i></b></li>
	 * <li style="list-style: none;"></li>
	 * <li style="list-style: none;">
	 * Number of the {@code port} on which a {@link Connection} will be opened.
	 * 
	 * </li>
	 * </ul>
	 */
	private static int port = 3306;
	/**
	 * <ul>
	 * <li><b><i>database</i></b></li>
	 * <li style="list-style: none;"></li>
	 * <li style="list-style: none;">
	 * {@link String} name of the {@code database} to which a {@link Connection} will be opened.
	 * 
	 * </li>
	 * </ul>
	 */
	private static String database = "file";	
	/**
	 * <ul>
	 * <li><b><i>usr</i></b></li>
	 * <li style="list-style: none;"></li>
	 * <li style="list-style: none;">
	 * {@link String} {@code user name} required to open a {@link Connection}. 
	 * 
	 * </li>
	 * </ul>
	 */
	private static String usr = "root";
	/**
	 * <ul>
	 * <li><b><i>pwd</i></b></li>
	 * <li style="list-style: none;"></li>
	 * <li style="list-style: none;">
	 * {@link String} {@code password} required to open a {@link Connection}. 
	 * 
	 * </li>
	 * </ul>
	 */
	private static String pwd = "";
	/**
	 * <ul style="margin-top: 24px;">
	 * <li><b><i>configureConnection</i></b></li>
	 * <li style="list-style: none;"></li>
	 * <li style="list-style: none;">
	 * <p style="margin-left: 7px;">{@code public void configureConnection({@link String} server, int port, {@link String} database, {@link String} usr, {@link String} pwd)}</p>
	 * <br>	
	 * Sets the name of the {@code server}, the {@code port} number, {@code database} name, {@code user name} and {@code password} required to open a {@link Connection}. 
	 * 
	 * @param server   - name of the {@code server}
	 * @param port	   - {@code port} number
	 * @param database - name of the {@code database}
	 * @param usr	   - {@code user name}
	 * @param pwd	   - {@code password}
	 * </li>
	 * </ul>
	 */
	public void configureConnection(String server, int port, String database, String usr, String pwd) {
		ConnectionManager.server = server;
		ConnectionManager.port = port;
		ConnectionManager.database = database;
		ConnectionManager.usr = usr;
		ConnectionManager.pwd = pwd;
	}
	/**
	 * <ul style="margin-top: 24px;">
	 * <li><b><i>createConnection</i></b></li>
	 * <li style="list-style: none;"></li>
	 * <li style="list-style: none;">
	 * <p style="margin-left: 7px;">{@code public} {@link Connection} {@code createConnection() throws} {@link Exception}</p>
	 * <br>	
	 * Opens and returns a {@link Connection} using {@link DriverManager} with driver {@code com.mysql.jdbc.Driver}.
	 * 
	 * @return a {@code Connection} with the preset configuration
	 * @throws if any error occurred while trying to open the connection
	 * </li>
	 * </ul>
	 */
    @SuppressWarnings("finally")
	public Connection createConnection() throws Exception{
        Connection con = null;
        
        try {
        	Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://"+server+":"+Integer.toString(port)+"/"+database, usr, pwd); 
        } catch (SQLException ex) {
        	String msg = "Error in edu.bbu.cshu.wmpc.dao.jdbc.ConnectionManager.createConnection:\n"+ex.getMessage();
            EventLogger.INSTANCE.writeToLog(Level.ERROR, msg);
        } finally {
            return con;
        }
    }
    /**
	 * <ul style="margin-top: 24px;">
	 * <li><b><i>closeConnection</i></b></li>
	 * <li style="list-style: none;"></li>
	 * <li style="list-style: none;">
	 * <p style="margin-left: 7px;">{@code public void closeConnection(}{@link Connection} {@code con) throws} {@link Exception}</p>
	 * <br>	
	 * If the {@code Connection} exists and is not yet closet then it will be closed. 
	 * 
	 * @param con - the {@code Connection} that is to be closed
	 * @throws if any error occurred while trying to close the given connection
	 * </li>
	 * </ul>
	 */
    public void closeConnection(Connection con) throws Exception{
        try {
        	if (con != null && !con.isClosed()) {
        		con.close();
        	}
        } catch (SQLException ex) {
        	String msg = "Error in edu.bbu.cshu.wmpc.dao.jdbc.ConnectionManager.closeConnection:\n"+ex.getMessage();
            EventLogger.INSTANCE.writeToLog(Level.ERROR, msg);
        }
    }
}
